home *** CD-ROM | disk | FTP | other *** search
/ C# & Game Programming - A…er's Guide (2nd Edition) / Buono 2nd Ed.iso / GameClasses / AnimatedImage.cs next >
Text File  |  2004-09-07  |  11KB  |  302 lines

  1. /* AnimatedImage.cs: Contains the AnimatedImage class, which is a 
  2.  * generic class capable of holding and doing animation logic for
  3.  * an image. You can sub-class this object to customize its behavior.
  4.  */
  5.  
  6. using System;
  7. using System.Drawing;
  8.  
  9. namespace GameClasses {
  10.     public class AnimatedImage {
  11.         // Constants
  12.         public const int DEFAULTSTEP = 3;
  13.         public const int SOUTHWEST = 1;
  14.         public const int SOUTH = 2;
  15.         public const int SOUTHEAST = 3;
  16.         public const int EAST = 4;
  17.         public const int NORTHEAST = 5;
  18.         public const int NORTH = 6;
  19.         public const int NORTHWEST = 7;
  20.         public const int WEST = 8;
  21.  
  22.         // Member data
  23.         public int imageWidth;  // Width of the image
  24.         public int imageHeight; // Height of the image
  25.         public int imagePosX;   // X Position in containing object
  26.         public int imagePosY;   // Y Position in containing object
  27.         public Image imageData; // The image itself
  28.  
  29.         public int imageOffsetX; // facilitates "relative" positioning
  30.         public int imageOffsetY; // by placing image at a specified 
  31.         // offset to PosX, PosY.
  32.  
  33.         public int animationStep; // How much the image should move/step
  34.  
  35.         public Rectangle constraintBox; // Our sandbox to play in
  36.         public int direction; // the current direction the image is heading
  37.         public bool isActive; // whether the image should be animated
  38.         public AnimatedImage owner; // if it's controlled by another image
  39.  
  40.     #region Constructors
  41.         public AnimatedImage() : this(0,0) {} // default constructor
  42.  
  43.         public AnimatedImage(int posX, int posY) {
  44.             imagePosX = posX;
  45.             imagePosY = posY;
  46.             isActive = false;
  47.             animationStep = DEFAULTSTEP;
  48.         }
  49.     #endregion
  50.  
  51.         // Animate() performs all position movements on the object.
  52.         virtual public void Animate() {
  53.             switch(direction) {
  54.                 case SOUTHWEST:
  55.                     this.imagePosX -= animationStep;
  56.                     this.imagePosY += animationStep;
  57.                     break;
  58.                 case SOUTH:
  59.                     this.imagePosY += animationStep;
  60.                     break;
  61.                 case SOUTHEAST:
  62.                     this.imagePosX += animationStep;
  63.                     this.imagePosY += animationStep;
  64.                     break;
  65.                 case WEST:
  66.                     this.imagePosX -= animationStep;
  67.                     break;
  68.                 case EAST:
  69.                     this.imagePosX += animationStep;
  70.                     break;
  71.                 case NORTHWEST:
  72.                     this.imagePosX -= animationStep;
  73.                     this.imagePosY -= animationStep;
  74.                     break;
  75.                 case NORTH:
  76.                     this.imagePosY -= animationStep;
  77.                     break;
  78.                 case NORTHEAST:
  79.                     this.imagePosX += animationStep;
  80.                     this.imagePosY -= animationStep;
  81.                     break;
  82.             }
  83.         }
  84.    
  85.  
  86.         // Change the image to be scaled to width, height
  87.         virtual public void RescaleImage(int width, int height) {
  88.             imageWidth = width;
  89.             imageHeight = height;
  90.         }
  91.  
  92.  
  93.         // Display the contained image using the given Graphics object
  94.         virtual public void Display(Graphics g) {
  95.             g.DrawImage(imageData, imagePosX + imageOffsetX, 
  96.                 imagePosY + imageOffsetY, 
  97.                 imageWidth, imageHeight);
  98.         }
  99.  
  100.         // Determines if the point is within the confines of this image
  101.         virtual public bool ContainsPoint(int x, int y) {
  102.             return (x >= imagePosX && x <= imagePosX + imageWidth &&
  103.                 y >= imagePosY && y <= imagePosY + imageHeight);
  104.         }
  105.  
  106.         // Determines if the given rectangle intersects this image
  107.         virtual public bool Intersects(int x, int y, 
  108.             int width, int height) {
  109.             // simple variable renaming
  110.             int x2 = x + width, y2 = y + height, _x = imagePosX;
  111.             int _y = imagePosY, _x2 = imagePosX + imageWidth;
  112.             int _y2 = imagePosY + imageHeight;
  113.  
  114.             return ((x >= _x && x <= _x2) || (x2 >= _x && x2 <= _x2) ||
  115.                 (_x >= x && _x <= x2) || (_x2 >= x && _x2 <= x2)) &&
  116.                 ((y >= _y && y <= _y2) || (y2 >= _y && y2 <= _y2) ||
  117.                 (_y >= y && _y <= y2) || (_y2 >= y && _y2 <= y2));
  118.         }
  119.  
  120.         virtual public bool Intersects(AnimatedImage img) {
  121.             return Intersects(img.imagePosX, img.imagePosY, 
  122.                 img.imageWidth, img.imageHeight);
  123.         }
  124.  
  125.         // Moves the image to be within the specified boundary.
  126.         // Returns whether any position changes were made.
  127.         virtual public bool ConstrainToBox() {
  128.             bool retVal = false;
  129.  
  130.             if (!isActive)
  131.                 return false;
  132.  
  133.             if (imagePosX < constraintBox.X) {
  134.                 imagePosX = constraintBox.X;
  135.                 retVal = true;
  136.             } else if ((imagePosX + imageWidth) > 
  137.                 (constraintBox.X + constraintBox.Width)) {
  138.                 imagePosX = constraintBox.X + 
  139.                     constraintBox.Width - imageWidth;
  140.                 retVal = true;
  141.             } 
  142.       
  143.             if (imagePosY < constraintBox.Y) {
  144.                 imagePosY = constraintBox.Y;
  145.                 retVal = true;
  146.             } else if ((imagePosY + imageHeight) > 
  147.                 (constraintBox.Y + constraintBox.Height)) {
  148.                 imagePosY = constraintBox.Y + 
  149.                     constraintBox.Height - imageHeight;
  150.                 retVal = true;
  151.             }
  152.  
  153.             return retVal;
  154.         }
  155.  
  156.         // Basic deflection
  157.         // Returns whether any position changes were made.
  158.         virtual public bool Deflect(int x, int y, int width, int height) {
  159.             if (!isActive)
  160.                 return false;
  161.  
  162.             switch(direction) {
  163.                 case SOUTHWEST:
  164.                     if (imagePosX <= x) {
  165.                         direction = SOUTHEAST;
  166.                     } else {
  167.                         direction = NORTHWEST;
  168.                     }
  169.                     break;
  170.                 case SOUTH:
  171.                     direction = NORTH;
  172.                     break;
  173.                 case SOUTHEAST:
  174.                     if ((imagePosX + imageWidth) >= (x + width)) {
  175.                         direction = SOUTHWEST;
  176.                     } else {
  177.                         direction = NORTHEAST;
  178.                     }
  179.                     break;
  180.                 case WEST:
  181.                     direction = EAST;
  182.                     break;
  183.                 case EAST:
  184.                     direction = WEST;
  185.                     break;
  186.                 case NORTHWEST:
  187.                     if (imagePosY <= y) {
  188.                         direction = SOUTHWEST;
  189.                     } else {
  190.                         direction = NORTHEAST;
  191.                     }
  192.                     break;
  193.                 case NORTH:
  194.                     direction = SOUTH;
  195.                     break;
  196.                 case NORTHEAST:
  197.                     if (imagePosY <= y) {
  198.                         direction = SOUTHEAST;
  199.                     } else {
  200.                         direction = NORTHWEST;
  201.                     }
  202.                     break;
  203.             }
  204.             return true;
  205.         }
  206.  
  207.         // Deflect override -- deflects off of a given AnimatedImage
  208.         virtual public bool Deflect(AnimatedImage img) {
  209.             return Deflect(img.imagePosX, img.imagePosY, 
  210.                 img.imageWidth, img.imageHeight);
  211.         }
  212.  
  213.         // Deflect override -- deflects off of default constraint box
  214.         virtual public bool Deflect() {
  215.             return Deflect(constraintBox.X, constraintBox.Y, 
  216.                 constraintBox.Width, constraintBox.Height);
  217.         }
  218.  
  219.         virtual public bool WrapInBox() {
  220.             bool retVal = false;
  221.  
  222.             if (!isActive)
  223.                 return false;
  224.  
  225.             if (imagePosX < constraintBox.X) {
  226.                 imagePosX = constraintBox.X + constraintBox.Width - imageWidth;
  227.                 retVal = true;
  228.             } else if ((imagePosX + imageWidth) > 
  229.                 (constraintBox.X + constraintBox.Width)) {
  230.                 imagePosX = constraintBox.X;
  231.                 retVal = true;
  232.             } 
  233.       
  234.             if (imagePosY < constraintBox.Y) {
  235.                 imagePosY = constraintBox.Y + constraintBox.Height - 
  236.                     imageHeight;
  237.                 retVal = true;
  238.             } else if ((imagePosY + imageHeight) > 
  239.                 (constraintBox.Y + constraintBox.Height)) {
  240.                 imagePosY = constraintBox.Y;
  241.                 retVal = true;
  242.             }
  243.  
  244.             return retVal;
  245.         }
  246.  
  247.         // reposition the object in a random place within its constraintBox
  248.         public void RandomizePosition() {
  249.             Random rnd = new Random();
  250.             imagePosX = rnd.Next(constraintBox.Width - imageWidth);
  251.             imagePosY = rnd.Next(constraintBox.Height - imageHeight);
  252.             direction = rnd.Next(1, 8);
  253.         }
  254.  
  255.         // Take whatever direction you are going and go the 
  256.         // opposite direction
  257.         virtual public void ReverseDirection() {
  258.             switch (direction) {
  259.                 case SOUTHWEST:
  260.                     direction = NORTHEAST;
  261.                     break;
  262.                 case SOUTH:
  263.                     direction = NORTH;
  264.                     break;
  265.                 case SOUTHEAST:
  266.                     direction = NORTHWEST;
  267.                     break;
  268.                 case WEST:
  269.                     direction = EAST;
  270.                     break;
  271.                 case EAST:
  272.                     direction = WEST;
  273.                     break;
  274.                 case NORTHWEST:
  275.                     direction = SOUTHEAST;
  276.                     break;
  277.                 case NORTH:
  278.                     direction = SOUTH;
  279.                     break;
  280.                 case NORTHEAST:
  281.                     direction = SOUTHWEST;
  282.                     break;
  283.             }
  284.         }
  285.  
  286.         // Rotate the direction by the specified amount
  287.         // increment is in 45 degree steps
  288.         virtual public void RotateDirection(int increment) {
  289.             int result = increment + direction;
  290.  
  291.             if (result > 8) {
  292.                 result %= 8;
  293.                 if (result == 0)
  294.                     result = 8;
  295.             } else if (result < 1) {
  296.                 result = (result % 8) + 8;
  297.             }
  298.       
  299.             direction = result;
  300.         }
  301.     }
  302. }